<?php
namespace app\common\model;

use think\Db;

class ConfigModel extends BaseModel
{
    protected $name = 'config'; //定义用户表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改

    /*读取器*/


    /*修改器*/

    /**
     * 更新缓存
     */
    public function updateCache()
    {
        try{
            $config = $this->order('config_sort asc')->select();
            $data   = [];
            foreach ($config as $v) {
                $name        = strtoupper($v['name']);
                $data[$name] = $v['value'];
            }
            cache('webconfig', $data);
            return true;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }

    /**
     * 批量修改配置项
     */
    public function batchSave($data)
    {
        try{
            if (!is_array($data)) {
                $this->error = '数据不能为空！';
                return false;
            }
            $this->allowField(true)->saveAll($data);
            return true;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }

    /**
     * 添加配置项
     * @param $data
     */
    public function addData($data)
    {
        try{
            $info = $this->where('name',$data['name'])->find();
            if ($info) {
                $this->error = '该配置项已存在!';
                return false;
            }
            $this->isUpdate(false)->allowField(true)->save($data);
            return true;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }

    /**
     * 删除数据
     * @param $id
     * @return bool
     */
    public function delData($id)
    {
        try{
            $this->destroy($id);
            $this->updateCache();
            return true;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }

    /**
     * 按分组获取配置项
     * @return mixed
     */
    public function getConfig($uid)
    {
        try{
            $groupList = Db::table('config_group')->order("group_sort")->column('*', 'id'); //获取配置组
            foreach ($groupList as $k => $v) {
                $map['group_id'] = $v['id'];
                $config          = Db::table('config')->where($map)->order("config_sort ASC")->select()->toArray();
                if ($config) {
                    foreach ($config as $i => $c) {
                        $func                = '_' . $c['show_type'];//text radio select group textarea
                        if($func=='_upimg'){
                            $config[$i]['_html'] = $this->$func($c,$uid);
                        }else{
                            $config[$i]['_html'] = $this->$func($c);
                        }
                    }
                }
                $groupList[$k]['_config'] = $config;
            }
            return $groupList;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }

    /**
     * 文本框
     * @param $config
     * @return string
     */
    private function _text($config)
    {
        return "<input type=\"text\" class=\"form-control input-sm\" name=\"config[{$config['id']}][value]\" value=\"{$config['value']}\" />";
    }

    /**
     * 单选框
     * @param $config
     * @return string
     */
    private function _radio($config)
    {
        $info = explode(',', $config['info']);
        $html = '';
        foreach ($info as $radio) {
            $data    = explode('|', $radio);//[0]值如1  [1]描述如开启
            $checked = $data[0] == $config['value'] ? ' checked="checked" ' : '';
            $html .= "<label class=\"radio-inline i-checks\"><input type=\"radio\" name=\"config[{$config['id']}][value]\" value=\"{$data[0]}\" $checked /> {$data[1]}</label> ";
        }
        return $html;
    }

    /**
     * 多行文本框
     * @param $config
     * @return string
     */
    private function _textarea($config)
    {
        return "<textarea class=\"form-control input-sm\" rows=\"3\" name=\"config[{$config['id']}][value]\">{$config['value']}</textarea>";
    }

    /**
     * 下拉框
     * @param $config
     * @return string
     */
    private function _select($config)
    {
        $info = explode(',', $config['info']);
        $html = "<select name=\"config[{$config['id']}][value]\" class=\"form-control input-sm\">";
        foreach ($info as $radio) {
            $data     = explode('|', $radio);//[0]值如1  [1]描述如开启
            $selected = $data[0] == $config['value'] ? ' selected="selected" ' : '';
            $html .= "<option value='{$data[0]}' $selected> {$data[1]}</option> ";
        }
        $html .= "</select>";
        return $html;
    }

    /**
     * 图片上传框
     * @param $config
     * @return string
     */
    private function _upimg($config,$uid)
    {
        return "{:widget('common/Form/singleImg',['config[{$config['id']}][value]','{$config['value']}','{$uid}'])}";
    }
}